<meta title="选品库管理" args="shopcat"/>
<div class="p">
    <div class="clearfix m-b-xs">
        <form class="form-inline m-b-xs pull-left" id="form">
            <input type="text" autocomplete="off" class="form-control input-sm" name="name" placeholder="输入选品库名称查询"/>
            <div class="input-group input-group-sm">
                <input type="text" autocomplete="off" s="" s-data="shopcat.id" id="shopcatId" class="form-control input-sm" name="shopcatId" placeholder="分类ID" style="width: 120px"/>
                <div class="input-group-btn">
                    <input type="button" value="选择" class="btn btn-default" s-click="selectShopcat()"/>
                </div>

            </div>
            <select class="form-control input-sm" s="select" s-build="S.get('/api/channel/list')" name="channelId">
                <option value="">频道</option>
            </select>
            <select class="form-control input-sm" s="select"
                    name="type"
                    s-build="S.get('/api/enum/valuesMap/core.enums.ItemSelectionType')">
                <option value="">选品方式</option>
            </select>
            优选：
            <label class="s-checkbox-switch">
                <input autocomplete="off" type="checkbox" name="recommend" value="true" />
                <span class="s-checkbox-indicator"></span>
            </label>
            <select class="form-control input-sm" s="select"
                    name="status"
                    s-build="S.get('/api/enum/valuesMap/core.enums.ItemSelectionStatus?igNames=DELETED')">
                <option value="" selected>状态</option>
            </select>
            <input autocomplete="off" type="hidden" name="page" id="page" value="1"/>
            <input type="button" class="btn btn-primary btn-sm" value="查询" s-click="$page.node.val(1);$itemSelectionsGrid.refresh()"/>
        </form>
        <div class="pull-right">
            <a href="javascript:;" class="btn btn-default btn-sm"
               s-click="batchSetConfig()">设置类型参数
            </a>
            <a href="javascript:;" class="btn btn-default btn-sm"
               s-click="batchFetch()">批量采集
            </a>
            <a href="javascript:;" class="btn btn-default btn-sm"
               s-click="add()">
                <i class="fa fa-plus"></i> 新增
            </a>
            <a href="javascript:;" class="btn btn-default btn-sm s-loading-icon "
               s-click="return $itemSelectionsGrid.refresh()">
                <i class="fa fa-refresh s-loading-spinner"></i>
            </a>
        </div>
    </div>
    <div id="itemSelectionsGrid"
         s='datac,nda,check'
         s-data="search()">
        <table class="table table-striped table-bordered table-middle table-hover">
            <tr>
                <th>
                    <label class="s-checkbox">
                        <input autocomplete="off" type="checkbox" s-check-role="checkall-h"/>
                        <span class="s-checkbox-indicator"></span>
                    </label>
                </th>
                <th>ID</th>
                <th>名称</th>
                <th>类型</th>
                <th>频道ID</th>
                <th>类目ID</th>
                <th>自动</th>
                <th>最大页数</th>
                <th>抓取时间</th>
                <th>抓取数量</th>
                <th style="max-width: 200px;">错误日志</th>
                <th>状态</th>
                <th class="text-right">操作</th>
            </tr>
            <tbody s="loop" s-data-filter="'results'">
            <tr s-loop-role="empty">
                <td colspan="13">暂无数据</td>
            </tr>
            <tr s-loop-role="row" s="datac" s-check-role="i" class="itemSelection">
                <td>
                    <label class="s-checkbox">
                        <input autocomplete="off" type="checkbox" s-check-role="h"/>
                        <span class="s-checkbox-indicator"></span>
                    </label>
                </td>
                <td name="id"></td>
                <td name="name"></td>
                <td name="typeTitle"></td>
                <td s="tpl">
                    <span name="channelName">{%=this.channelName%}</span>
                    {%if(this.clearChannelItem){%}
                    <label class="label-warning label">清</label>
                    {%}%}
                </td>
                <td name="shopcatTitle"></td>
                <td s="tpl">
                    {%if(this.autoFetch){%}
                    <label class="label label-success">自动</label>
                    {%} else {%}
                    <label class="label label-default">关闭</label>
                    {%}%}
                </td>
                <td name="maxPage"></td>
                <td name="lastFetchTime"></td>
                <td name="lastFetchNum"></td>
                <td name="dangerMsg" style="max-width: 200px;"></td>
                <td s="tpl">
                    {%if(this.status=='ACTIVE'){%}
                    <label class="label label-success">激活</label>
                    {%}else{%}
                    <label class="label label-default">锁定</label>
                    {%}%}
                </td>
                <td class="text-right" s="tpl">
                    <input autocomplete="off" type="button" class="btn btn-xs btn-info" value="采集商品" s-click="collect(this)"/>
                    {%if(this.status === 'ACTIVE'){%}
                    <input autocomplete="off" type="button" class="btn btn-xs btn-default" value="锁定" s-click="setStatus(this, 'LOCKED')"/>
                    {%}else{%}
                    <input autocomplete="off" type="button" class="btn btn-xs btn-success" value="激活" s-click="setStatus(this, 'ACTIVE')"/>
                    {%}%}
                    <input autocomplete="off" type="button" class="btn btn-xs btn-default" value="编辑" s-click="edit(this)"/>
                    <input autocomplete="off" type="button" class="btn btn-xs btn-danger" value="删除" s-click="setStatus(this, 'DELETED')"/>
                </td>
            </tr>
            </tbody>
            <tfoot>
            <tr>
                <td colspan="13" class="text-right">
                    共有<span name="total"></span>个数据
                    <ul s="pagination"
                        s-pagination="{action:function(){$page.node.val(this); $itemSelectionsGrid.refresh()}}"
                        class="pagination pagination-sm">
                    </ul>
                </td>
            </tr>
            </tfoot>
        </table>
    </div>
</div>
<script type="text/javascript">

    function add() {
        S.popupOpen("/itemSelection/itemSelectionAdd.html").done(function(rs) {
            rs && $itemSelectionsGrid.refresh();
        })
    }

    function collect($this) {
        var data = $this.parent().data();
        S.get("/api/itemSelection/collect/" + data.id).done(function(rs) {
            if (rs.success) {
                S.toast(data.name + '采集完成', 'success')
                if(datas.length > 0) {
                    doFetch()
                } else {
                    S.toast('批量采集完成', 'success')
                }
            } else {
                S.toast(rs.errMsg, 'danger')
            }
        })
    }

    function setStatus($this, status) {
        var data = $this.parent().data();
        S.confirm("确定操作吗？").done(function() {
            S.put("/api/itemSelection/status/" + data.id + "/" + status).done(function() {
                $itemSelectionsGrid.refresh();
            })
        })
    }

    function search() {
        return S.get('/api/itemSelection/search', $form.node.serialize()).done(function(rs) {
            if(rs.total == 0) {
                if(shopcat) {
                    S.popupOpen("/itemSelection/itemSelectionAdd.html", {shopcat: shopcat, name: shopcat.pathName}).done(function(rs) {
                        if(rs) {
                            $itemSelectionsGrid.refresh();
                        }
                    })
                }
            }
        })
    }

    function edit($this) {
        var data = $this.parent().data();
        S.popupOpen("/itemSelection/itemSelectionAdd.html", {itemSelection: data}).done(function(rs) {
            rs && $itemSelectionsGrid.refresh();
        })
    }

    function selectShopcat() {
        S.popupOpen("/shopcat/shopcatSelector.html").done(function(shopcats, path) {
            if(shopcats && shopcats.length) {
                $shopcatId.node.val(shopcats[0].id)
            }
        })
    }

    function shopcatBatchAdd() {
        S.popupOpen("/shopcat/shopcatSelector.html").done(function(shopcats, path) {
            if(shopcats && shopcats.length) {
                function doAdd() {
                    var _shopcat = shopcats.pop()
                    var name = path.concat(_shopcat).map(function(cat) {
                        return cat.title
                    }).join("-")
                    S.popupOpen("/itemSelection/itemSelectionAdd.html", {shopcat: _shopcat, name: name}).done(function(rs) {
                        if(rs) {
                            $itemSelectionsGrid.refresh();
                        }
                        if(shopcats.length > 0) {
                            doAdd()
                        }
                    })
                }
                doAdd()
            }
        })
    }

    function batchSetConfig() {
        var datas = $itemSelectionsGrid.getCheckedData();
        if (datas.length == 0) {
            S.toast("请选择要操作的数据", "warning");
            return;
        }
        S.popupOpen('/itemSelection/TBK_DG_MATERIAL_OPTIONAL_CONFIG.html').done(function(config) {
            if(config) {
                function doSet() {
                    S.popupOpen("/itemSelection/itemSelectionAdd.html", {itemSelection: datas.shift(), config: config}).done(function(rs) {
                        if(datas.length > 0) {
                            doSet()
                        }
                    })
                }
                doSet()
            }
        })

    }

    function batchFetch() {
        var datas = $itemSelectionsGrid.getCheckedData();
        if (datas.length == 0) {
            S.toast("请选择要操作的数据", "warning");
            return;
        }
        function doFetch() {
            var data = datas.shift()
            S.get("/api/itemSelection/collect/" + data.id).done(function(rs) {
                if (rs.success) {
                    S.toast(data.name + '采集完成', 'success')
                    if(datas.length > 0) {
                        doFetch()
                    } else {
                        S.toast('批量采集完成', 'success')
                    }
                } else {
                    S.toast(rs.errMsg, 'danger')
                }
            })
        }
        doFetch()
    }
</script>
